package data.dictionary.saver;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Iterator;

import data.dictionary.Dictionary_Memory;

import tools.Pair;

public class DictionarySaverInOctaveFormat {
	private String filename;
	private Dictionary_Memory<String>  dico;

	public DictionarySaverInOctaveFormat(String filename, Dictionary_Memory<String> dico) {
		super();
		this.filename = filename;
		this.dico = dico;
	}

	public void save() {

		try {
			FileOutputStream output = new FileOutputStream(filename);
			PrintStream p = new PrintStream(output);

			p.println("# Created by JAVA");
			p.println("# name: dico");
			p.println("# type: cell");
			p.println("# rows: " + dico.size());
			p.println("# columns: 1");

			Iterator<String> iter = dico.iterator();
			
			while (iter.hasNext()) {
				String mot = iter.next();
				p.println("# name: <cell-element>");
				p.println("# type: string");
				p.println("# elements: 1");
				p.println("# length: " + mot.length());

				p.println(mot);
			}
			
			output.close();
		} catch (IOException e) {
			System.err.println("Can't open file " + filename
					+ " for writting... Saving aborted");

		}

	}

}
